/*
  Description:事件埋点
  Author: Mao
  Date: 2025-04-21
*/
import {
    eventTrackingInsert
} from '@/api/user.js'

const eventTypes = {
    /**
     * 切换材质事件
     */
    SWITCH_MATERIAL : "switch_material",
    /**
     * 切换机型事件
     */
    SWITCH_MODEL : "switch_model",

    /** AI定制事件 */
    AI_CUSTOMIZATION : "ai_custom",

    /** caseform列表事件 */
 
    CASEFORM_LIST : "caseform_list",

    /**
     * DIY定制事件
     */
    DIY_CUSTOMIZATION : "diy_custom",

    /**
     * 个人中心事件
     */
    PERSONAL_CENTER : "personal_center",

}
class eventTracking {
	constructor(params) {
        lock: false,

		this.state = {
			eventName: "",
            deviceId: "",
            pageUrl: "",
            platform: "",
            screenWidth: "",
            screenHeight: "",
            osVersion: "",
            appVersion: "",
            extraData: {},
		}
	}


	async event(type, pageUrl='pages/example' , extraData = {}) {
        let that = this;
        if (that.lock) return
        that.lock = true
        try {
            // ...原有代码...
            let systemData = uni.getSystemInfoSync(); // 获取系统数据
            console.log("获取系统数据",systemData);
			that.state.eventName = eventTypes[type];
            that.state.deviceId = systemData.deviceId;
            that.state.pageUrl = pageUrl;
            that.state.platform = systemData.platform;
            that.state.screenWidth = systemData.screenWidth;
            that.state.screenHeight = systemData.screenHeight;
            that.state.osVersion = systemData.osVersion;
            that.state.appVersion = systemData.appVersion;
			that.state.extraData = extraData;
            await eventTrackingInsert(that.state)
        } finally {
            that.lock = false
        }

		// let that = this;
		// return new Promise( async (resolve, reject) => {
  
		// 	await eventTrackingInsert(that.state).then(res => {
		// 		resolve(res);
		// 	}).catch(err => {
		// 		reject(err);
		// 	})
		// });
    }
}
export default new eventTracking()